import axios from 'axios'

const api = axios.create({
  baseURL: 'http://localhost:3000/api', // 后端地址
})

// 登录
export function login(account, passwd) {
  return api.post('/login', { account, passwd }).then(res => res.data)
}

// 获取共享文件夹
export function getShares(sid) {
  return api.get('/shares', { params: { sid } }).then(res => res.data)
}

// 获取文件夹下文件
export function getFiles(sid, path) {
  return api.get('/files', { params: { sid, path } }).then(res => res.data)
}

// 上传文件
export function uploadFile(sid, path, file) {
  const formData = new FormData()
  formData.append('sid', sid)
  formData.append('path', path)
  formData.append('file', file)
  return api.post('/upload', formData, {
    headers: { 'Content-Type': 'multipart/form-data' }
  }).then(res => res.data)
}

// 移动文件或文件夹
export function moveFile(sid, src, dest) {
  return api.post('/move', {
    sid,
    src,
    dest
  }).then(res => res.data)
}

// 新建文件夹
export function createFolder(sid, parent, name) {
  return api.post('/create-folder', {
    sid,
    parent,
    name
  }).then(res => res.data)
}

// 删除文件或文件夹
export function deleteFile(sid, path) {
  return api.post('/delete', {
    sid,
    path
  }).then(res => res.data)
}

// 查询删除进度
export function deleteStatus(sid, taskid) {
  return api.get('/delete-status', { params: { sid, taskid } }).then(res => res.data)
}

// 复制文件或文件夹
export function copyFile(sid, src, dest) {
  return api.post('/copy', {
    sid,
    src,
    dest
  }).then(res => res.data)
}

// 下载文件（返回下载链接，前端直接跳转即可）
export function getDownloadUrl(sid, path) {
  return `${api.defaults.baseURL}/download?sid=${encodeURIComponent(sid)}&path=${encodeURIComponent(path)}`
}

// 直连群晖的文件上传（仅测试用，不推荐生产环境）
export function qunhuiDirectUpload(token, path, file, filename) {
  const formData = new FormData();
  formData.append('path', path);
  formData.append('create_parents', 'true');
  formData.append('file', file, filename);
  // 其它参数如 overwrite 可按需添加
  return axios.post(
    `http://192.168.0.177:5000/webapi/entry.cgi?api=SYNO.FileStation.Upload&method=upload&version=2&_sid=${token}`,
    formData,
    { headers: { 'Content-Type': 'multipart/form-data' } }
  ).then(res => res.data);
}

// 生成壳子分享链接
export function createShare(sid, path, filename, owner, expiresAt) {
  return api.post('/share', { sid, path, filename, owner, expiresAt }).then(res => res.data)
} 